﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ControlAccessUser.Presenter;

namespace ControlAccessUser.Presentation
{
    /// <summary>
    /// Lógica de interacción para SearchUser.xaml
    /// </summary>
    public partial class SearchUser : Window, ISearchUserView
    {
        PresenterSearchUser pSearchUser;
        public SearchUser(Model.User User)
        {
            InitializeComponent();
            pSearchUser = new PresenterSearchUser(this);
            UserLogged = User;
        }

        public int UserIdSearch
        {
            get
            {
                if (string.IsNullOrEmpty(txbxUserId.Text))
                    return -1;
                else
                    return int.Parse(txbxUserId.Text);
            }
            set{ }
        }

        public string UserFirstNameSearch
        {
            get
            {
                if (!string.IsNullOrEmpty(txbxUserFirstName.Text))
                    return txbxUserFirstName.Text;
                else
                    return "";
                
            }
            set{}
        }

        public string UserLastNameSearch
        {
            get
            {
                if (!string.IsNullOrEmpty(txbxUserLastName.Text))
                    return txbxUserLastName.Text;
                else
                    return "";
            }
            set { }
        }

        public string UserNameSearch
        {
            get
            {
                if (!string.IsNullOrEmpty(txbxUserName.Text))
                    return txbxUserName.Text;
                else
                    return "";
            }
            set { }
        }

        public ICollection<Model.User> UsersFound
        {           
            set
            {
                dataGridUsers.ItemsSource = value;
            }
        }     

        public Model.User  UserLogged
        {
	        get ;
	        set ;
        }

        public string ErrorMessage
        {
            get 
            {
                return null; 
            }
            set
            {
                MessageBox.Show(value,"Error",MessageBoxButton.OK,MessageBoxImage.Error);
            }
        }

        public string SystemMessage
        {
            get 
            {
                return null;
            }
            set
            {
                lblSystemMessage.Content = value;
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            pSearchUser.SearchUserByRelativeFields();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dataGridUsers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            pSearchUser.GoDetailsLogins();
        }

        public void GoUserSelected(Model.User User)
        {
            (new ReportsOfLogins(User)).ShowDialog();
        }


        Model.User ISearchUserView.DataSelectedUser
        {
            get
            {
                if (dataGridUsers.SelectedItem != null)
                    return ((Model.User)dataGridUsers.SelectedItem);
                else
                    return null;
            }
            set { }
        }

        public bool UserReallyGoOut
        {
            get
            {
                if (MessageBox.Show("Quiere cerrar la sesion del usuario seleccionado??", "ATENCION!!", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                {
                    return true;
                }
                else
                    return false;
            }
            set { }
        }

        private void btnLogoutUserSelected_Click(object sender, RoutedEventArgs e)
        {
            pSearchUser.LogoutSelectedUser();
        }

        private void dataGridUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lblSystemMessage.Content = "";
        }
    }
}
